import requests
from bs4 import BeautifulSoup
from .models import Feight


def the_res():
    try:
        res = requests.get(r'http://sz.58.com/chuzu/?PGTID=0d100000-0000-4d83-44af-fedb8aee05c2&ClickID=1')
        data = BeautifulSoup(res.text, 'html.parser')
        name = data.select('li > div[class="des"] > h2 > a')
        name_res = []
        room = data.select('p[class="room"]')
        money = data.select('div[class="money"] > b')
        address = data.select('p[class="add"]')
        address_res = []
        L = []
        for i in address:
            address_res.append(i.find_all('a'))
        for i in name:
            try:
                if i['title'] == '1':
                    pass
            except:
                name_res.append(i)
        t = Feight.objects
        for i in range(len(name_res)):
            t1 = name_res[i].string.strip()
            t2 = room[i].string.strip()
            t3 = money[i].string.strip()
            t4 = ''
            for j in address_res[i]:
                t4 += j.string.strip()
            # L.append([t1,t2,t3,t4])
            t.create(room_titile=t1, room_size=t2, room_monet=t3,room_address=t4)
        return L
    except:
        return 1